home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / MemFun1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  955 b   |  48 lines

  1. //: C21:MemFun1.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Applying pointers to member functions
  7. #include "../purge.h"
  8. #include <algorithm>
  9. #include <vector>
  10. #include <iostream>
  11. #include <functional>
  12. using namespace std;
  13.  
  14. class Shape {
  15. public:
  16.   virtual void draw() = 0;
  17.   virtual ~Shape() {}
  18. };
  19.  
  20. class Circle : public Shape {
  21. public:
  22.   virtual void draw() {
  23.     cout << "Circle::Draw()" << endl;
  24.   }
  25.   ~Circle() {
  26.     cout << "Circle::~Circle()" << endl;
  27.   }
  28. };
  29.  
  30. class Square : public Shape {
  31. public:
  32.   virtual void draw() {
  33.     cout << "Square::Draw()" << endl;
  34.   }
  35.   ~Square() {
  36.     cout << "Square::~Square()" << endl;
  37.   }
  38. };
  39.  
  40. int main() {
  41.   vector<Shape*> vs;
  42.   vs.push_back(new Circle);
  43.   vs.push_back(new Square);
  44.   for_each(vs.begin(), vs.end(), 
  45.     mem_fun(&Shape::draw));
  46.   purge(vs);
  47. } ///:~
  48.